<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2023-01-29 13:58:41
 * @LastEditors: 李小晴 2689279066@qq.com
 * @LastEditTime: 2023-11-13 23:25:54
 * @FilePath: \pc\sys\src\views\Asset\VaccineInject\Index.vue
 * Copyright (c) 2023 by 李天惊 uf_lee@163.com, All Rights Reserved. 
-->
<script setup lang="ts">
import { reactive, ref, watch } from 'vue'
import { ElButton } from 'element-plus'
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'

import { sysDictRelsApi } from '@/api/dict'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { useTable } from '@/hooks/web/useTableTest'
// 获取字典
import { useCache } from '@/hooks/web/useCache'

import { dbVaccineDetailedexportApi, dbVaccineDrugDetailedListApi } from '@/api/forms'
import { zcPastureListAdminApi } from '@/api/fa/pasture'

const { wsCache } = useCache()
const dict: any = wsCache.get('dict')
// 字典通过value获取label
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

const dictRelsData = ref() // 联动数据

const searchRef = ref() // 搜索的标签

const defaultTime = ref<[Date, Date]>([
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59)
])

// 获取联动关系
const getDictRels = async () => {
  const res = await sysDictRelsApi({
    p_type: 'breed',
    c_type: 'breed_sub'
  })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    dictRelsData.value = res.data
  }
}

const { register, tableObject, methods } = useTable<
  {
    total: number
    list: any[]
  },
  any
>({
  getListApi: dbVaccineDrugDetailedListApi,
  response: {
    data: 'data',
    count: 'count'
  }
})
const { setSearchParams } = methods
const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: '序号',
    type: 'index',
    width: 60,
    table: {
      show: true
    },
    form: {
      show: false
    },
    detail: {
      show: false
    }
  },
  {
    field: 'pasture_org_id',
    label: '牧场名称',
    width: 150,
    search: {
      component: 'DialogRefer',
      componentProps: {
        getListApi: zcPastureListAdminApi,
        response: { count: 'count', data: 'data' },
        title: '选择创建人（双击选择）',
        nameField: 'pasture_org_name',
        relNameField: 'org_name',
        valueField: 'org_id',
        crudSchemas: [
          { field: 'org_name', label: '牧场名称', search: { show: true } }
          // { field: 'org_id', label: '牧场ID', search: { show: true } }
        ]
      },
      show: true
    },
    table: {
      show: false
    }
  },
  {
    field: 'pasture_name',
    label: '牧场名称',
    width: 150
  },
  {
    field: 'vaccine_inject_order_code',
    label: '疫苗注射记录单号',
    width: 150,
    search: {
      show: true
    }
  },
  {
    field: 'type',
    label: '类型',
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('medical_drug_type', cellValue)
    },
    width: 150,
    search: {
      component: 'Select',
      componentProps: {
        options: dict.medical_drug_type
      },
      show: false
    }
  },
  {
    field: 'drug_name',
    label: '药品名称',
    width: 150,
    search: {
      show: false
    }
  },
  {
    field: 'medication_name',
    label: '药品名称',
    width: 150,
    search: {
      show: false
    }
  },
  {
    field: 'amount',
    label: '金额',
    width: 150
  },
  {
    field: 'medication_number',
    label: '用药数量',
    width: 150
  },
  {
    field: 'state',
    label: '状态',
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('vaccine_inject_order_detailed_state', cellValue)
    },
    width: 150,
    search: {
      component: 'Select',
      componentProps: {
        options: dict.vaccine_inject_order_detailed_state
      },
      show: true
    }
  },
  {
    field: 'inject_date',
    label: '注射日期',
    width: 170,
    form: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        disabled: true,
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    },
    search: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        'is-range': true,
        'unlink-panels': true,
        rangeSeparator: 'To',
        type: 'daterange',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        defaultTime: defaultTime
      }
    }
  },
  {
    field: 'create_time',
    label: '创建日期',
    width: 170,
    form: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        disabled: true,
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    },
    search: {
      show: true,
      component: 'DatePicker',
      componentProps: {
        'is-range': true,
        'unlink-panels': true,
        rangeSeparator: 'To',
        type: 'daterange',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        defaultTime: defaultTime
      }
    }
  },
  {
    field: 'create_user_name',
    label: '创建人',
    width: 100,
    form: {
      show: false
    },
    search: {
      show: true
    }
  },
  {
    field: 'note',
    label: '备注',
    width: 180,
    form: {
      show: false
    }
  }
  // {
  //   field: 'action',
  //   width: 200,
  //   fixed: 'right',
  //   label: '操作',
  //   form: {
  //     show: false
  //   },
  //   detail: {
  //     show: false
  //   }
  // }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

/****************************** 监听设置固定条件 start ******************************/
// const newFixedSearch = ref({})
watch(
  () => tableObject.tableList,
  () => {
    // jieguo()
  }
)
/****************************** 监听设置固定条件 end ******************************/

const init = async () => {
  // 列表查询
  // await getList()
  // 获取关联关系
  await getDictRels()
}
init()

const exportClick = async () => {
  dialogTableVisible.value = true
  let aaa: any = tableObject.paramsObj.params
  console.log(aaa, tableObject.count)

  let res: any = await dbVaccineDrugDetailedListApi({
    pasture_org_id: aaa.pasture_org_id,
    create_time: aaa.create_time,
    create_user_name: aaa.create_user_name,
    inject_date: aaa.inject_date,
    state: aaa.state,
    vaccine_inject_order_code: aaa.vaccine_inject_order_code
  })
    .catch(() => {})
    .finally(() => {})
  if (res) {
    FormsNumber.value = Math.ceil(res.count / 10000)
  }
}

const FormsNumber = ref(0)
const dialogTableVisible = ref(false)

const Export = async (index) => {
  let aaa: any = tableObject.paramsObj.params
  console.log(aaa, index)
  let { data }: any = await dbVaccineDetailedexportApi({
    request: {
      pasture_org_id: aaa.pasture_org_id,
      create_time: aaa.create_time,
      create_user_name: aaa.create_user_name,
      inject_date: aaa.inject_date,
      state: aaa.state,
      vaccine_inject_order_code: aaa.vaccine_inject_order_code,
      pageNo: index,
      pageSize: '10000'
    },
    layout_data: {
      file_name: '疫苗注射药品明细列表',
      column_cell_start: 'A2',
      column_data: [
        {
          title: '序号',
          width: 5,
          cell_type: 'key'
        },
        {
          title: '牧场名称',
          width: 20,
          cell_type: 'string',
          give_type: 'variable',
          give_value: 'pasture_name'
        },
        {
          title: '疫苗记录单号',
          width: 20,
          cell_type: 'string',
          give_type: 'variable',
          give_value: 'vaccine_inject_order_code'
        },
        {
          title: '类型',
          width: 20,
          cell_type: 'dict',
          dict_type: 'medical_drug_type',
          give_type: 'variable',
          give_value: 'type'
        },
        {
          title: '药品名称',
          width: 20,
          cell_type: 'string',
          give_type: 'variable',
          give_value: 'medication_name'
        },
        {
          title: '金额',
          width: 15,
          cell_type: 'number',
          give_type: 'variable',
          give_value: 'amount'
        },
        {
          title: '用药数量',
          width: 15,
          cell_type: 'number',
          give_type: 'variable',
          give_value: 'medication_number'
        },
        {
          title: '注射时间',
          width: 20,
          cell_type: 'date',
          give_type: 'variable',
          give_value: 'inject_date'
        },
        {
          title: '状态',
          width: 10,
          cell_type: 'dict',
          dict_type: 'vaccine_inject_order_state',
          give_type: 'variable',
          give_value: 'state'
        },
        {
          title: '备注',
          width: 20,
          cell_type: 'string',
          give_type: 'variable',
          give_value: 'note'
        },
        {
          title: '创建人',
          width: 20,
          cell_type: 'string',
          give_type: 'variable',
          give_value: 'create_user_name'
        },
        {
          title: '创建时间',
          width: 20,
          cell_type: 'datetime',
          give_type: 'variable',
          give_value: 'create_time'
        }
      ],
      custom: [
        {
          cell_type: 'string',
          cell: 'A1:L1',
          give_type: 'format',
          give_value: '疫苗注射药品明细列表',
          horizontal: 'center',
          border: true,
          size: '16'
        }
      ]
    }
  })
    .catch(() => {})
    .finally(() => {
      // saveLoading.value = false
    })
  if (data) {
    window.open(data.url)

    console.log(data, Math.ceil(data.count / 10000))
    // FormsNumber.value = Math.ceil(data.count / 10000)
  }
}
/****************************** 初始化 end ******************************/
</script>

<template>
  <ContentWrap>
    <Search
      ref="searchRef"
      :schema="allSchemas.searchSchema"
      @search="setSearchParams"
      @reset="setSearchParams"
    />

    <div v-show="tableObject.paramsObj.params">
      <div class="mb-10px w-[100%] flex justify-end">
        <ElButton type="primary" @click="exportClick()"> 导出EXCEL </ElButton>
      </div>
      <Table
        v-model:pageSize="tableObject.pageSize"
        v-model:currentPage="tableObject.currentPage"
        :columns="allSchemas.tableColumns"
        :data="tableObject.tableList"
        :loading="tableObject.loading"
        :border="true"
        :pagination="{
          total: tableObject.count
        }"
        @register="register"
        :selection="false"
      />
    </div>
  </ContentWrap>

  <el-dialog v-model="dialogTableVisible" title="疫苗主档导出详情">
    <div style="margin: 20px" v-for="(item, index) in FormsNumber" :key="index">
      <span style="margin: 20px">疫苗主档 - {{ item }}</span>
      <ElButton type="primary" @click="Export(item)">导出</ElButton>
    </div>
  </el-dialog>
</template>
